clear all

cd "~\Data"

use "study1_raw.dta"


*attention
recode check1 1=1 2/4=0
rename Conspiracy_5 check2
recode check2 1=1 2/5=0
alpha check1 check2, item casewise gen(CheckSum)
tab CheckSum

*PID
gen PID=pid
recode PID 1=2 2=6 3/4=4
recode PID 2=1 if sd==1
recode PID 6=7 if sr==1
recode PID 4=3 if pind==1
recode PID 4=5 if pind==2
gen PIDext=abs(PID-4)
gen ideoext=abs(ideo-4)

*Demos
recode sex 2/3=0, gen(male)
recode attend 1=6 2=5 3=4 4=3 5=2 6=1


*acquiescence
drop hs*DO*
drop Violence*DO*
drop Conspiracy*DO*

*sexism
foreach var of varlist hs_f* {
	recode `var' 1=5 2=4 4=2 5=1
}
egen HSf=rowmean(hs_f*)
egen HSr=rowmean(hs_r*)
label variable HSf "Hostile Sexism - Forward Items"
label variable HSr "Hostile Sexism - Reversed Items"

*HS forward: agree=sexist
gen HSforward=. 
replace HSforward=1 if HSf!=.
replace HSforward=0 if HSr!=.
label variable HSforward "Direction of Hostile Sexism Items"
label define forwardl 0 "Reversed" 1 "Forward"
label values HSforward forwardl

egen HSfull=rowfirst(HSf HSr)
label variable HSfull "Hostile Sexism - All Items"
ttest HSfull, by(HSforward)

*RR
foreach var of varlist rr_f* {
	recode `var' 1=5 2=4 4=2 5=1
}
egen RRf=rowmean(rr_f*)
egen RRr=rowmean(rr_r*)
label variable RRf "Racial Resentment - Forward Items"
label variable RRr "Racial Resentment - Reversed Items"

*RRforward: agree=racist
gen RRforward=.
replace RRforward=1 if RRf!=.
replace RRforward=0 if RRr!=.
label variable RRforward "Direction of Racial Resentment Items"
label values RRforward forwardl

egen RRfull=rowfirst(RRr RRf)
label variable RRfull "Racial Resentment - All Items"
ttest RRfull, by(RRforward)

*violence: agree=violent
foreach var of varlist Violence* {
	recode `var' 1=5 2=4 4=2 5=1
}
egen Violent=rowmean(Violence*)
label variable Violent "Support for Political Violence"

*RR & HS consistency
gen HS_RR_match=.
replace HS_RR_match=0 if (RRforward==0 & HSforward==1)
replace HS_RR_match=0 if (RRforward==1 & HSforward==0)
replace HS_RR_match=1 if (RRforward==0 & HSforward==0)
replace HS_RR_match=1 if (RRforward==1 & HSforward==1)

*conspiracy: agree=conspiracy
foreach var of varlist Conspiracy* {
	recode `var' 1=5 2=4 4=2 5=1
}
egen ConspPre=rowmean(Conspiracy*)
label variable ConspPre "Conspiratorial Predispositions"


*cleaning
keep PID ideo PIDext ideoext check* CheckSum sex attend male HS* RR* Violent ConspPre
drop HS_RR_match 


** generating differences in correlations **

gen V_RRf=.
gen V_RRr=.

gen V_HSf=.
gen V_HSr=.

gen C_RRf=.
gen C_RRr=.

gen C_HSf=.
gen C_HSr=.

gen RRf_HSf=.
gen RRf_HSr=.
gen RRr_HSf=.
gen RRr_HSr=.

set seed 98058
forval x=1/1000 {
preserve
bsample

qui cor Violent RRf
local VRf=r(rho) in `x'
qui cor Violent RRr
local VRr=r(rho) in `x'

qui cor ConspPre RRf
local CRf=r(rho) in `x'
qui cor ConspPre RRr
local CRr=r(rho) in `x'

qui cor Violent HSf
local VHf=r(rho) in `x'
qui cor Violent HSr
local VHr=r(rho) in `x'

qui cor ConspPre HSf
local CHf=r(rho) in `x'
qui cor ConspPre HSr
local CHr=r(rho) in `x'

qui cor RRf HSf
local RfHf=r(rho) in `x'
qui cor RRf HSr
local RfHr=r(rho) in `x'
qui cor RRr HSf
local RrHf=r(rho) in `x'
qui cor RRr HSr
local RrHr=r(rho) in `x'

restore

qui replace V_RRf=`VRf' in `x'
qui replace V_RRr=`VRr' in `x'

qui replace C_RRf=`CRf' in `x'
qui replace C_RRr=`CRr' in `x'

qui replace V_HSf=`VHf' in `x'
qui replace V_HSr=`VHr' in `x'

qui replace C_HSf=`CHf' in `x'
qui replace C_HSr=`CHr' in `x'

qui replace RRf_HSf=`RfHf' in `x'
qui replace RRf_HSr=`RfHr' in `x'
qui replace RRr_HSf=`RrHf' in `x'
qui replace RRr_HSr=`RrHr' in `x'


di `x'
}


gen V_RR_diff=V_RRf-V_RRr
gen C_RR_diff=C_RRf-C_RRr

gen V_HS_diff=V_HSf-V_HSr
gen C_HS_diff=C_HSf-C_HSr

gen RR_HS_fffr_diff=RRf_HSf-RRf_HSr
gen RR_HS_ffrf_diff=RRf_HSf-RRr_HSf

gen RR_HS_rrfr_diff=RRr_HSr-RRf_HSr
gen RR_HS_rrrf_diff=RRr_HSr-RRr_HSf


gen cor1=.
gen cor1se=.
gen cor2=.
gen cor2se=.
gen var1=""
gen var2=""

gen diff=.
gen se=.
gen lo=.
gen hi=.

gen cor1var=""
gen cor2var=""

sum V_RRf
replace cor1=r(mean) in 1
replace cor1se=r(sd) in 1
sum V_RRr
replace cor2=r(mean) in 1
replace cor2se=r(mean) in 1

replace cor1var="Vf_Rf" in 1
replace cor2var="Vf_Rr" in 1

sum V_RR_diff
replace diff=r(mean) in 1
replace se=r(sd) in 1
_pctile V_RR_diff, p(2.5 97.5)
replace lo=r(r1) in 1
replace hi=r(r2) in 1

sum C_RRf
replace cor1=r(mean) in 2
replace cor1se=r(sd) in 2
sum C_RRr
replace cor2=r(mean) in 2
replace cor2se=r(mean) in 2

replace cor1var="Cf_Rf" in 2
replace cor2var="Cf_Rr" in 2

sum C_RR_diff
replace diff=r(mean) in 2
replace se=r(sd) in 2
_pctile C_RR_diff, p(2.5 97.5)
replace lo=r(r1) in 2
replace hi=r(r2) in 2

sum V_HSf
replace cor1=r(mean) in 3
replace cor1se=r(sd) in 3
sum V_HSr
replace cor2=r(mean) in 3
replace cor2se=r(mean) in 3

replace cor1var="Vf_Sf" in 3
replace cor2var="Vf_Sr" in 3

sum V_HS_diff
replace diff=r(mean) in 3
replace se=r(sd) in 3
_pctile V_HS_diff, p(2.5 97.5)
replace lo=r(r1) in 3
replace hi=r(r2) in 3

sum C_HSf
replace cor1=r(mean) in 4
replace cor1se=r(sd) in 4
sum C_HSr
replace cor2=r(mean) in 4
replace cor2se=r(mean) in 4

replace cor1var="Cf_Sf" in 4
replace cor2var="Cf_Sr" in 4

sum C_HS_diff
replace diff=r(mean) in 4
replace se=r(sd) in 4
_pctile C_HS_diff, p(2.5 97.5)
replace lo=r(r1) in 4
replace hi=r(r2) in 4

sum RRf_HSf
replace cor1=r(mean) in 5
replace cor1se=r(sd) in 5
sum RRf_HSr
replace cor2=r(mean) in 5
replace cor2se=r(mean) in 5

replace cor1var="Rf_Sf" in 5
replace cor2var="Rf_Sr" in 5

sum RR_HS_fffr_diff
replace diff=r(mean) in 5
replace se=r(sd) in 5
_pctile RR_HS_fffr_diff, p(2.5 97.5)
replace lo=r(r1) in 5
replace hi=r(r2) in 5

sum RRf_HSf
replace cor1=r(mean) in 6
replace cor1se=r(sd) in 6
sum RRr_HSf
replace cor2=r(mean) in 6
replace cor2se=r(mean) in 6

replace cor1var="Rf_Sf" in 6
replace cor2var="Rr_Sf" in 6

sum RR_HS_ffrf_diff
replace diff=r(mean) in 6
replace se=r(sd) in 6
_pctile RR_HS_ffrf_diff, p(2.5 97.5)
replace lo=r(r1) in 6
replace hi=r(r2) in 6

sum RRr_HSr
replace cor1=r(mean) in 7
replace cor1se=r(sd) in 7
sum RRf_HSr
replace cor2=r(mean) in 7
replace cor2se=r(mean) in 7

replace cor1var="Rr_Sr" in 7
replace cor2var="Rf_Sr" in 7

sum RR_HS_rrfr_diff
replace diff=r(mean) in 7
replace se=r(sd) in 7
_pctile RR_HS_rrfr_diff, p(2.5 97.5)
replace lo=r(r1) in 7
replace hi=r(r2) in 7

sum RRr_HSr
replace cor1=r(mean) in 8
replace cor1se=r(sd) in 8
sum RRr_HSf
replace cor2=r(mean) in 8
replace cor2se=r(mean) in 8

replace cor1var="Rr_Sr" in 8
replace cor2var="Rr_Sf" in 8

sum RR_HS_rrrf_diff
replace diff=r(mean) in 8
replace se=r(sd) in 8
_pctile RR_HS_rrrf_diff, p(2.5 97.5)
replace lo=r(r1) in 8
replace hi=r(r2) in 8

gen var_rr=0
gen var_viol=0
gen var_consp=0
gen var_hs=0

replace var_rr=1 if (_n==1 | _n==2 | _n==5 | _n==6 | _n==7 | _n==8)
replace var_hs=1 if (_n>2)
replace var_viol=1 if (_n==1 | _n==3)
replace var_consp=1 if (_n==2 | _n==4)

recode var_* (0=.) if _n>8
recode var_* (1=.) if _n>8

gen lo196=diff-1.96*se
gen hi196=diff+1.96*se

gen x=_n
replace x=. if _n>8

keep cor1 cor2 cor1se cor2se diff se lo hi x var_* cor1var cor2var
drop if _n>8

save "study1_cors.dta", replace
